//
//  WYMomentCommentItemViewModel.m
//  FriendCircleDemo
//
//  Created by WY on 2018/10/9.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentCommentItemViewModel.h"

@interface WYMomentCommentItemViewModel ()

/// 评论模型
@property (nonatomic, readwrite, strong) WYComment *comment;

@end
@implementation WYMomentCommentItemViewModel

-(instancetype)initWithComment:(WYComment *)comment
{
    if (self = [super init]) {
        self.comment = comment;
        self.type = WYMomentContentTypeComment;
        
        /// 正文
        NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] init];
        
        /// : 正文富文本 （还需要匹配超链接 表情 电话号码 @xx）
        NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"：%@",comment.text]];
        textAttr.color = [UIColor blackColor];
        textAttr.font = SYSTEMFONT(14.0);
        
        /// 点击的高亮的
        YYTextBorder *border = [YYTextBorder new];
        border.cornerRadius = 0;
        border.insets = UIEdgeInsetsMake(0, -1, 0, -1);
        border.fillColor = TextHighlightBackgroundColor;
        
        /// XXX （来源）
        NSMutableAttributedString *fromAttr = [[NSMutableAttributedString alloc] initWithString:comment.fromUser.screenName];
        fromAttr.color = MomentScreenNameTextColor;
        fromAttr.font = SYSTEMFONT(14.0);
        /// 点击高亮
        YYTextHighlight *fromHighlight = [YYTextHighlight new];
        // 将用户数据带出去
        fromHighlight.userInfo = @{WYMomentUserInfoKey:comment.fromUser};
        [fromHighlight setBackgroundBorder:border];
        [fromAttr setTextHighlight:fromHighlight range:fromAttr.rangeOfAll];
        
        /// 是否有 回复xxx
        if (comment.toUser) {
            /// 回复
            NSMutableAttributedString *replyAttr = [[NSMutableAttributedString alloc] initWithString:@"回复"];
            replyAttr.color = textAttr.color;
            replyAttr.font = textAttr.font;
            
            /// xxx（目标）
            NSMutableAttributedString *toAttr = [[NSMutableAttributedString alloc] initWithString:comment.toUser.screenName];
            toAttr.color = fromAttr.color;
            toAttr.font = fromAttr.font;
            /// 高亮
            YYTextHighlight *toHighlight = [[YYTextHighlight alloc] init];
            [toHighlight setBackgroundBorder:border];
            /// 将用户数据带出去
            toHighlight.userInfo = @{WYMomentUserInfoKey:comment.toUser};
            [toAttr setTextHighlight:toHighlight range:toAttr.rangeOfAll];
            
            /// 拼接数据 (A回复B)
            [replyAttr appendAttributedString:toAttr];
            [fromAttr appendAttributedString:replyAttr];
        }
        
        /// 拼接 （A回复B：xxxx   或者  A：xxxx）
        [contentAttr appendAttributedString:fromAttr];
        [contentAttr appendAttributedString:textAttr];
        
        /// 统一配置
        contentAttr.lineBreakMode = NSLineBreakByCharWrapping;
        contentAttr.alignment = NSTextAlignmentLeft;
        
        /// 文本布局
        CGFloat limitWidth = WYMomentCommentViewWidth()-2*WYMomentCommentViewContentLeftOrRightInset;
        YYTextContainer *contentLableContainer = [YYTextContainer containerWithSize:CGSizeMake(limitWidth, MAXFLOAT)];
        contentLableContainer.maximumNumberOfRows = 0;
        YYTextLayout *contentLableLayout = [YYTextLayout layoutWithContainer:contentLableContainer text:contentAttr.copy];
        self.contentLableLayout = contentLableLayout;
        
        
        /// ----------- 尺寸属性 -----------
        CGFloat contentLableW = contentLableLayout.textBoundingSize.width;
        CGFloat contentLableH = contentLableLayout.textBoundingSize.height;
        self.contentLableFrame = CGRectMake(WYMomentCommentViewContentLeftOrRightInset, WYMomentCommentViewContentTopOrBottomInset, contentLableW, contentLableH);
        self.cellHeight = CGRectGetMaxY(self.contentLableFrame)+WYMomentCommentViewContentTopOrBottomInset;
    }
    return self;
}
@end
